package get

import (
	"database/sql"

	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
)

func Get_testrecord(r *gin.Engine) {
	var err error
	var db *sql.DB
	var code, str string
	db, err = sql.Open("postgres", "port=5432 user=postgres password=system dbname=databasedesign sslmode=disable")
	if err != nil {
		panic(err)
	}

	var qid int
	var question, aa, bb, s string
	var mbti string = "2333"

	r.GET("/gettest", func(c *gin.Context) {
		code = c.Query("code")
		str = "SELECT * FROM question where qid='" + code + "'"
		rows, err := db.Query(str)
		if err != nil {
			panic(err)
		}

		for rows.Next() {

			err = rows.Scan(&qid, &question, &aa, &bb)
			if err != nil {
				panic(err)
			}

		}
		// fmt.Printf("%s\n", feature5)
		// 获取参数
		c.JSON(200, gin.H{
			"qid":      qid,
			"question": question,
			"aa":       aa,
			"bb":       bb,
			"str":      str,
			// "mbti":     mbti,
		})
	})

	r.GET("/getlog", func(c *gin.Context) {
		s = c.Query("ss")

		var ee, ii, ss, nn, tt, ff, jj, pp int = 0, 0, 0, 0, 0, 0, 0, 0

		/* 问卷记录 					需要被传参*/

		/* 选项：0表示A，1标识B		 需要被传参*/

		if s[0] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[1] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[2] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[3] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[4] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[5] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[6] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[7] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[8] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[9] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[10] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[11] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[12] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[13] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[14] == '0' {

		} else {
			pp = pp + 1
		}

		if s[15] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[16] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[17] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[18] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[19] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[20] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[21] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[22] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[23] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[24] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[25] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[26] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[27] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[28] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[29] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[30] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[31] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[32] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[33] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[34] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[35] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[36] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[37] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[38] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[39] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[40] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[41] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[42] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[43] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[44] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[45] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[46] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[47] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[48] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[49] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[50] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[51] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[52] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[53] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[54] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[55] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[56] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[57] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[58] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[59] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[60] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[61] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[62] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[63] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[64] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[65] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[66] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[67] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[68] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[69] == '0' {
			jj = jj + 1
		} else {
			pp = pp + 1
		}

		if s[70] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[71] == '0' {
			ii = ii + 1
		} else {
			ee = ee + 1
		}

		if s[72] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[73] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[74] == '0' {
			tt = tt + 1
		} else {
			nn = nn + 1
		}

		if s[75] == '0' {
			pp = pp + 1
		} else {
			jj = jj + 1
		}

		if s[76] == '0' {
			ee = ee + 1
		} else {
			ii = ii + 1
		}

		if s[77] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[78] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[79] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[80] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[81] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[82] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[83] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[84] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[85] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[86] == '0' {
			nn = nn + 1
		} else {
			ss = ss + 1
		}

		if s[87] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[88] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[89] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		if s[90] == '0' {
			ff = ff + 1
		} else {
			tt = tt + 1
		}

		if s[91] == '0' {
			tt = tt + 1
		} else {
			ff = ff + 1
		}

		if s[92] == '0' {
			ss = ss + 1
		} else {
			nn = nn + 1
		}

		mbti = ""

		if ee > ii {
			mbti = mbti + "E"
		} else {
			mbti = mbti + "I"
		}

		if ss > nn {
			mbti = mbti + "S"
		} else {
			mbti = mbti + "N"
		}

		if tt > ff {
			mbti = mbti + "T"
		} else {
			mbti = mbti + "F"
		}

		if jj > pp {
			mbti = mbti + "J"
		} else {
			mbti = mbti + "P"
		}

		c.JSON(200, gin.H{
			"mbti": mbti,
			//"ss":   ss,
		})
	})

	r.GET("/getres", func(c *gin.Context) {
		var mname string
		str1 := "SELECT mname FROM mbti where mal='" + mbti + "'"
		rows, err := db.Query(str1)
		if err != nil {
			panic(err)
		}
		for rows.Next() {
			err = rows.Scan(&mname)
			if err != nil {
				panic(err)
			}
		}

		c.JSON(200, gin.H{
			"mname": mname,
			"mal":   mbti,
		})
	})
}
